﻿/*
 * This is our basic enemy.
 * This guy simply tries to aim at the player while going at a predefined speed
 */

using System;
using AgateLib.InputLib;
using AgateLib.Sprites;
using AiAsteriods.Game.Entities.Base;
using AiAsteriods.Game.Entities.OpenSteerDotNet;
using AiAsteriods.Game.State;
using AiAsteriods.Game.Tools;

namespace AiAsteriods.Game.Entities.Enemies {
  class Enemy : EntityAbstract {
    private int hitPoints = 1;
    private int turnreduction = 3000;
    private bool isDead;

    public Enemy(Sprite s, int x, int y, double speed, double rot, double radius, int collisionDamage, GameStateAbstract state) : base(s, x, y, rot, speed, radius, collisionDamage, 0, state) {
      isDead = false;
      mySprite.SetScale(0.8, 0.8);
    }

    public override void Update(int delta) {
      double angleDifferenceToPlayer = trackPlayer();
      angleDifferenceToPlayer = angleDifferenceToPlayer / turnreduction;
      Drot = ((angleDifferenceToPlayer * 2) * delta);

      base.Update(delta);
    }

    public override void Draw() {
      base.Draw();
    }

    public override void Collide(EntityAbstract e) {
      if (!isDead && e is Projectile.Bullet) {
        hitPoints += -e.CollisionDamage;
        if (hitPoints <= 0) {
          isDead = true;
          Global.GameStart.CurrentState.AddScoreForKill();
          //CircleExplosion explo = new CircleExplosion((int)this.X, (int)this.Y, 0, 0, myState);
          //myState.AddEntity(explo);
          base.Collide(e);
        }
      }
    }

    #region seekPlayer
    public double trackPlayer() {
      //	protected double seekPlayer() {		
      EntityAbstract p = myState.Player1;
      float x = (float)p.X;
      float y = (float)p.Y;
      Vector3 target = new Vector3(x, y, 0.0f);
      Vector3 seekVelocity;
      if (Keyboard.Keys[KeyCode.S])
        seekVelocity = p.steerForFlee(target);
      else
        seekVelocity = p.steerForSeek(target);
      double trot = Math.Atan2((seekVelocity.x - X), (seekVelocity.y - Y)) * RadToDegree;
      double rotaDiff = trot - Rotation;
      return (rotaDiff);
    }
    #endregion
  }
}
